package JPLXtandemGA;

import java.util.Formatter;
import java.util.Random;

public class IntegerParameter implements IParameter<IntegerParameter> {

	private final String name;
	private final int value;
	private final int lowerBound;
	private final int upperBound;
	private final Random RNG = new Random();

	public IntegerParameter(String name, int value, int lowerBound,
			int upperBound) {
		this.name = name;
		this.value = value;
		this.lowerBound = lowerBound;
		this.upperBound = upperBound;
	}

	@Override
	public IntegerParameter mutate() {
		int newValue = constraint(value + RNG.nextInt());
		return new IntegerParameter(name, newValue, lowerBound, upperBound);
	}

	private int constraint(int d) {
		if (d < lowerBound)
			return lowerBound;
		if (d > upperBound)
			return upperBound;
		return d;
	}

	@Override
	public String name() {
		return name;
	}

	@Override
	public IntegerParameter randomize() {
		int interval = (upperBound - lowerBound) + 1;
		int newValue = (int)(interval * RNG.nextDouble() + lowerBound);
		return new IntegerParameter(name, newValue, lowerBound, upperBound);
	}

	@Override
	public String xmlRepresentation() {
		Formatter fmt = new Formatter();
		fmt.format("%d", value);
		return Parameters.makeNote(name, fmt.toString());
	}
	@Override
	public String valueToString() {
		return Integer.toString(value);
	}

}